import wx from 'weixin-js-sdk'
import axios from 'axios'

export function getShareParamsInit (url, customerId, bizId, title, imgUrl, desc) {
  console.log('getShareParamsInit')
  let shareUrl = location.href.split('#')
  console.log(shareUrl)
  let reqUrl = shareUrl[0].toString()
  console.log('reqUrl')
  console.log(reqUrl + bizId)
  let sharedLink = shareUrl[0].toString() + '#' + url + '?id=' + bizId.toString()
  console.log(customerId)
  let config = {
    headers: {'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/json', 'Accept': 'application/json'},
    responseType: 'json',

    method: 'post'
  }
  let reqData = {
    'url': reqUrl,
    'customerId': customerId
  }
  axios.post('/weixin/jssdk/getShareParams', reqData, config).then(function (response) {
    console.log(response)
    console.log(response.data.data)
    let data = response.data.data
    let url = data.url
    console.log('data')
    console.log(data)
    console.log('url')
    console.log(url)
    wx.config({
      debug: false,
      appId: data.appid,
      timestamp: data.timestamp,
      nonceStr: data.nonceStr,
      signature: data.signature,
      jsApiList: [
        'checkJsApi',
        'onMenuShareTimeline',
        'onMenuShareAppMessage'
      ]
    })
    wx.ready(function () {
      var link = sharedLink
      console.log('分享朋友圈')
      wx.onMenuShareTimeline({
        title: title,
        link: link,
        imgUrl: imgUrl,
        trigger: function (res) {
          console.log(res)
        },
        success: function (res) {
          alert('shared success')
          console.log(res)
        },
        cancel: function (res) {
          alert('shared cancle')
          console.log(res)
        },
        fail: function (res) {
          alert(JSON.stringify(res))
          console.log(res)
        }
      })
      console.log('分享给好友')
      wx.onMenuShareAppMessage({
        title: title,
        desc: desc,
        link: link,
        imgUrl: imgUrl, // 自定义图标
        type: 'link',
        dataUrl: '',
        success: function () {
          console.log('用户确认分享后执行的回调函数')
        },
        cancel: function () {
          console.log('用户取消分享后执行的回调函数')
        }
      })
      wx.error(function (res) {
        alert(res.errMsg)
      })
    })
  }).catch(function (error) {
    console.log(error)
  })
}

wx.ready(function () {
  var link = window.location.href
  console.log('分享朋友圈')
  wx.onMenuShareTimeline({
    title: '这是一个自定义的标题！',
    link: link,
    imgUrl: '',
    trigger: function (res) {
      console.log(res)
    },
    success: function (res) {
      alert('shared success')
      console.log(res)
    },
    cancel: function (res) {
      alert('shared cancle')
      console.log(res)
    },
    fail: function (res) {
      alert(JSON.stringify(res))
      console.log(res)
    }
  })
  console.log('分享给好友')
  wx.onMenuShareAppMessage({
    title: '这是一个自定义的标题！',
    desc: '这是一个自定义的描述！',
    link: link,
    imgUrl: '', // 自定义图标
    type: 'link',
    dataUrl: '',
    success: function () {
      console.log('用户确认分享后执行的回调函数')
    },
    cancel: function () {
      console.log('用户取消分享后执行的回调函数')
    }
  })
  wx.error(function (res) {
    alert(res.errMsg)
  })
})
